package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.presenters.MeAccountPresenter;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的项目合伙人
 * Created by admin on 2017/10/29.
 */

public class MyProjectpartnerActivity extends BaseMvpActivity<MeAccountView, MeAccountPresenter> implements MeAccountView, View.OnClickListener {
    private ListView detail_list;
    private QuickAdapter<UserOBean.Data> adapter;
    private View rll_top, fl_Left, flIcon1;
    private TextView title;
    private ImageView icon1;
    private int type;//是否来自接单推荐1是0否
    private String role;

    @Override
    public MeAccountPresenter initPresenter() {
        return new MeAccountPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_me_account_detail);
        type = getIntent().getIntExtra(KEY_TYPE, 0);
        detail_list = (ListView) findViewById(R.id.detail_list);
        rll_top = findViewById(R.id.rll_top);
        rll_top.setVisibility(View.GONE);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        flIcon1 = findViewById(R.id.flIcon1);
        flIcon1.setOnClickListener(this);
        icon1 = (ImageView) findViewById(R.id.icon1);
        icon1.setVisibility(View.VISIBLE);
        icon1.setImageDrawable(getResources().getDrawable(R.drawable.i_search));
        title = (TextView) findViewById(R.id.title);
        title.setText("我的项目合伙人");
        role = "";
        if (type == 1) {//设计师
            title.setText("我的项目合伙人");
            role = "0";
        }

        if (type == 2) {//项目经理
            title.setText("我的项目合伙人");
            role = "1";
        }

        if (type == 3) {//监理
            title.setText("我的项目合伙人");
            role = "2";
        }

        if (type == 4) {//寻找设计分包伙伴传3
            title.setText("我的项目合伙人");
            role = "3";
        }

        if (type == 5) {//寻找项目分包伙伴传3
            title.setText("我的项目合伙人");
            role = "4";
        }
        presenter.onLoadMyPartner($this, "", role);
    }

    @Override
    public void initData() {
        presenter.setOncliclkLisener(new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case ACCOUNT_TYPE_COMMEN:
                        adapter = new QuickAdapter<UserOBean.Data>($this, R.layout.item_partner, (List<UserOBean.Data>) param[1]) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, final UserOBean.Data item) {

                                helper.setText(R.id.txtRolePhone, "(" + item.getPhone() + ")");
                                helper.setText(R.id.txtCity, item.getAddress());
                                helper.setText(R.id.txtRoleName, item.getUserName());

                                String role = item.getRoleId();

                                TagGroup tagGroup = helper.getView(R.id.tagGroup);
                                ArrayList<String> tags = new ArrayList<String>();
                                if (role != null) {
                                    tagGroup.setVisibility(View.VISIBLE);
                                    if (role.contains("0")) {
                                        tags.add("设计师");
                                    }

                                    if (role.contains("1")) {
                                        tags.add("项目经理");
                                    }

                                    if (role.contains("2")) {
                                        tags.add("监理");
                                    }

                                    if (role.contains("3")) {
                                        tags.add("体验店");
                                    }

                                    tagGroup.setTags(tags);
                                } else {
                                    tagGroup.setVisibility(View.GONE);
                                }


                                if (type > 0) {
                                    helper.getView().setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            if (type == 4 || type == 5){
                                                Utils.showDialog((Activity) context, "提示", "是否确认分包给该合伙人？", "", "", new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        Utils.dismissDialog();
                                                        switch (view.getId()) {
                                                            case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:
                                                                break;
                                                            case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                                                                Intent intent = new Intent();
                                                                intent.putExtra(KEY_PARAM, item);
                                                                setResult(RESULT_OK, intent);
                                                                finish();
                                                                break;
                                                        }
                                                    }
                                                });
                                            }else{
                                                Intent intent = new Intent();
                                                intent.putExtra(KEY_PARAM, item);
                                                setResult(RESULT_OK, intent);
                                                finish();
                                            }

                                        }
                                    });
                                }
                            }
                        };
                        detail_list.setAdapter(adapter);
                        break;

                }
            }
        });
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.flIcon1:
                startActivityForResult(new Intent($this, SearchActivity.class).putExtra(KEY_TYPE, SEARCH_TYPE_PARTNER), INTENT_SEARCH);
                break;
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case INTENT_SEARCH:
                    presenter.onLoadMyPartner($this, data.getStringExtra(KEY_KEYWORDS), role);//,type,data.getStringExtra(KEY_KEYWORDS)
                    break;
            }
        }
    }
}
